﻿using ProjBackEnd.Areas.Financial.Models;
using ProjBackEnd.DAL;
using ProjBackEnd.Lib;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;

namespace ProjBackEnd.Areas.Financial.Controllers
{
    /// <summary>
    /// 行政类费用操作
    /// </summary>
    public class AdmFeeOptController : ApiController
    {
        /// <summary>
        /// 获取报销单列表
        /// eg.：0000000001|TST1|WEB|20|1|0||0|2,3|2018-11-01||0
        /// </summary>
        /// <param name="key">用户id|公司代码|平台代码|每页记录数|当前页数（从1开始）|结算单id（0=所有）|制单人id（空值=所有）|客户id（0=所有）|结算单pid（逗号分隔，空值=所有）|建档起始时间（空值=所有）|建档结束时间（空值=所有）|账单id（0=所有）</param>
        /// <returns></returns>
        [HttpGet]
        [Route("api/financial/expenseopt")]
        public RespExpense GetExpense(string key)
        {
            RespExpense resp = new RespExpense();
            if (string.IsNullOrEmpty(key))
            {
                resp.statusCode = 90004;
                resp.statusMsg = "请求数据格式错误";
                return resp;
            }

            string strToken = DataUtil.CheckTokenValid(Request);
            if (strToken == "")
            {
                resp.statusCode = 90002;
                resp.statusMsg = "非法请求";
                return resp;
            }

            string[] strsKeyOriginal = DataUtil.PlaintextConfusion(key, DataUtil.enumDeEncryptType.Decrypt).Split('|');
            if (strsKeyOriginal.Length < 12)
            {
                resp.statusCode = 90004;
                resp.statusMsg = "请求数据格式错误";
                return resp;
            }

            string strMemberid = strsKeyOriginal[0];
            string strComcd = strsKeyOriginal[1];
            string strPlatcd = strsKeyOriginal[2];
            string strCreateid = strsKeyOriginal[6];
            string[] strsPid = strsKeyOriginal[8].Split(',');
            if (strsPid.Length == 1)
            {
                if (strsPid[0] == "") strsPid = new string[] { };
            }

            int iPageSize, iCurrPage, iFnid, iCusid, iBillid;
            if (!int.TryParse(strsKeyOriginal[3], out iPageSize) || !int.TryParse(strsKeyOriginal[4], out iCurrPage) || !int.TryParse(strsKeyOriginal[5], out iFnid) || !int.TryParse(strsKeyOriginal[7], out iCusid) || !int.TryParse(strsKeyOriginal[11], out iBillid))
            {
                resp.statusCode = 90004;
                resp.statusMsg = "请求数据格式错误";
                return resp;
            }

            DateTime? dtSdt = null;
            DateTime? dtEdt = null;
            try
            {

                if (!string.IsNullOrEmpty(strsKeyOriginal[9])) dtSdt = DateTime.Parse(strsKeyOriginal[9]);
                if (!string.IsNullOrEmpty(strsKeyOriginal[10])) dtEdt = DateTime.Parse(strsKeyOriginal[10]).Date.AddDays(1);
            }
            catch
            {
                resp.statusCode = 90004;
                resp.statusMsg = "请求数据格式错误";
                return resp;
            }

            if (!userLogic.CheckToken(strMemberid, strToken, strPlatcd))
            {
                resp.statusCode = 90001;
                resp.statusMsg = "用户令牌无效";
                return resp;
            }

            resp = financialLogic.GetExpense(strCreateid, strComcd, iPageSize, iCurrPage, iFnid, iCusid, strsPid, dtSdt, dtEdt, iBillid);
            return resp;
        }

        /// <summary>
        /// 更新报销单汇总信息
        /// </summary>
        /// <param name="_reqInfo">更新请求</param>
        /// <returns></returns>
        [HttpPost]
        [Route("api/financial/expenseopt")]
        public RespFinaNullResultInfo UpdateExpense(ReqExpense _reqInfo)
        {
            RespFinaNullResultInfo resp = new RespFinaNullResultInfo();
            if (!ModelState.IsValid)
            {
                string strErrmsg = DataUtil.GetModelStateError(ModelState);
                resp.statusCode = 90004;
                resp.statusMsg = "请求数据格式错误。" + strErrmsg;
                return resp;
            }

            string strToken = DataUtil.CheckTokenValid(Request);
            if (strToken == "")
            {
                resp.statusCode = 90002;
                resp.statusMsg = "非法请求";
                return resp;
            }

            if (!userLogic.CheckToken(_reqInfo.memberid, strToken, _reqInfo.platcd))
            {
                resp.statusCode = 90001;
                resp.statusMsg = "用户令牌无效";
                return resp;
            }

            resp = financialLogic.UpdateExpense(_reqInfo.memberid, _reqInfo.comcd, _reqInfo.expensebaseinfo);
            return resp;

        }



    }
}
